package com.bkitmobile.kesu.android.GUI;

import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Handler;
import android.text.Html;
import android.util.Log;
import com.appenginefan.toolkit.common.ClientSocketSubstitute;
import com.bkitmobile.kesu.android.connectServer.Request;
import com.bkitmobile.kesu.android.connectServer.ServiceCallback;
import com.bkitmobile.kesu.android.connectServer.staticClass;

public class ConfirmLoginDlg {
	private String username;
	private Builder dlg;
	private Context context;
	private boolean state;
	private final Handler handler = new Handler();
	public ConfirmLoginDlg(String username,Context context,boolean state)
	{
		this.state = state;
		this.username = username;
		this.context = context;
		String message = "User <b><font color=\"#3399ff\">"+username+"</font></b> already logged in, do you want to continue?";
		dlg = new AlertDialog.Builder(context)
        .setTitle("Confirm message")
        .setMessage(Html.fromHtml(message))
        .setPositiveButton("Yes", new OnClickListener() {
			
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					String []identities = {"ID"};
					String []atributies = {Request.CONFIRM_LOGIN};
					Request.sendAndRecieveData(identities, atributies, true,ConfirmLoginDlg.this.context, new ServiceCallback<String>() {
						@Override
						public void onSuccess(final String result) {
							handler.post(new Runnable() {
								public void run() {
									Intent intent = new Intent(ConfirmLoginDlg.this.context, FriendsList.class);
							    	int n = result.indexOf("success");
							    	intent.putExtra("state", ConfirmLoginDlg.this.state);
							    	intent.putExtra("list", result);
							    	intent.putExtra("username", ConfirmLoginDlg.this.username);
							    	staticClass.user = ConfirmLoginDlg.this.username;
							    	 try {
							 			staticClass.socket = new ClientSocketSubstitute(
							 					 	new URL(staticClass.socketURL),   // Connection URL
							 			            1000,                     // ping frequency
							 			            5                         // max. messages per ping
							 			        );
							 		} catch (MalformedURLException e1) {
							 			e1.printStackTrace();
							 		}
							    	ConfirmLoginDlg.this.context.startActivity(intent);
							    	((Activity)ConfirmLoginDlg.this.context).finish();
								}
							});
						}
						
						@Override
						public void onFailure(Exception exception) {
							handler.post(new Runnable() {
								
								@Override
								public void run() {
									// TODO Auto-generated method stub
									StaticDialogs.okButtonDialog(ConfirmLoginDlg.this.context
											, ConfirmLoginDlg.this.context.getString(R.string.disconnect));
								}
							});
							
						}
					});
					//Log.d("string", resp);
					
				}
			}
        )
        .setNegativeButton("No", new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
			}
        }
        );
	}
	public void show()
	{
		dlg.show();
	}
	
}
